<html>
<head><title> CS143 Project 1B </title></head>
<body>
<h1> MySQL Query Tester </h1>
<p> (Built on  4/18/2012 by Edvin Zohrabyan) </p>
<p> <big> Type a query to retrieve information from the tables below.
    Tables and fields are case sensitive!</big> </p> 

<table border=1 cellspacing=1 cellpadding=2 color="#00008B">
<tr align=center><td bgcolor="#99CCFF"><b>Tables & Attributes</b></td></tr>
<tr align=center><td>Actor(id, last, first, sex, dob, dod)</td></tr>
<tr align=center><td>Director(id, last, first, dob, dod)</td></tr>
<tr align=center><td>MaxMovieID(id)</td></tr>
<tr align=center><td>MaxPersonID(id)</td></tr>
<tr align=center><td>Movie(id, title, year, rating, company)</td></tr>
<tr align=center><td>MovieActor(mid, aid, role)</td></tr>
<tr align=center><td>MovieDirector(mid, did)</td></tr>
<tr align=center><td>MovieGenre(mid, genre)</td></tr>
<tr align=center><td>Review(name, time, mid, rating, comment)</td></tr>
</table>

<p>
<form action="./query.php" method="GET">
<textarea name="query" cols="60" rows="8"></textarea>
<input type="submit" value="Submit Query" />
</form>
</p>

<?php  

$sql= $_GET["query"];
if($sql){
   
   /* connection error handling */
   if (!$db_connection = mysql_connect("localhost", "cs143", "")) {
       echo 'Could not connect to mysql';
       exit;
   }

   mysql_select_db("CS143", $db_connection);

   $result = mysql_query($sql, $db_connection);

   if (!$result) {
      echo "DB Error, could not query the database\n";
      echo 'MySQL Error: ' . mysql_error();
      exit;  
   }

   /* create the table and display attribute names */

   echo "<h3><FONT COLOR='#CC0000'>Here is the result:</FONT> </h3>";


   echo "<table border=1 cellspacing=1 cellpadding=2>";

   /* get the number of attribute in the result */

   $num_fields = mysql_num_fields($result);

   echo "<tr align=center>";
   for ($i=0; $i < $num_fields; $i++)
      echo '<td><b>'.mysql_field_name($result, $i).'</b></td> ';
      echo '</tr> ';

   /* loop through the result and display the table */ 

   while ($row = mysql_fetch_row($result)) {

       $column = sizeof($row);
       echo "<tr align=center>";

       for ($i=0; $i<$column; $i++){
	   $cell = $row[$i];
	   echo "<td>$cell</td>";
       }
       echo "</tr>";
    }

    echo "</table>";

    /* free the result and close the connection */
    mysql_free_result($result);
    mysql_close($db_connection);

}
?>

</body>
</html>


